﻿using System;
using System.Collections.Generic;
using System.Text;
using Store.Model.Models;
using System.Linq;
using Store.ViewModel;

namespace Store.Dal
{
    public class PromotionDAL
    {
        private StoreContext db;
        public PromotionDAL(StoreContext _db)
        {
            db = _db;
        }
        //促销显示
        public List<Promotion> GetPromotions(string Ptitle = "", int Pstate = 2)
        {
            var linq = from s in db.Promotions
                       where s.PromoDelState==1
                       select s;
            if (!string.IsNullOrEmpty(Ptitle))
            {
                linq = linq.Where(s => s.Ptitle.Contains(Ptitle));
            }
            if (Pstate != 2)
            {
                linq = linq.Where(s => s.Pstate==Pstate);
            }
            return linq.ToList();
        }
        //促销表添加
        public int AddPromotions(Promotion pro)
        {
            db.Promotions.Add(pro);
            return db.SaveChanges();
        }
        //促销表删除
        public int DelPromotions(int id)
        {
            var linq = db.Promotions.Find(id);
            if (linq != null)
            {
                linq.PromoDelState = 0;
                return db.SaveChanges();
            }
            return -1;
        }
        //促销表修改
        public int UpdatePromotions(Promotion pro)
        {
            var linq = db.Promotions.Find(pro.PromotionId);
            if (linq != null)
            {
                linq.Ptitle = pro.Ptitle;
                linq.Particulars = pro.Particulars;
                linq.Pstate = pro.Pstate;
                linq.CreateTime = pro.CreateTime;
                return db.SaveChanges();
            }
            return -1;
        }
        //显示商品
        public List<CommodityModel> GetCommodities()
        {
            var linq = (from s in db.Commodities
                       join a in db.Typees on s.TypeId equals a.TypeId
                       where s.ComDelState == 1
                       select new CommodityModel
                       {
                           CommodityId = s.CommodityId,
                           CommodityImg = s.CommodityImg,
                           CommodityName = s.CommodityName,
                           ArticleNumber = s.ArticleNumber,
                           CommodityPrice = s.CommodityPrice,
                           TypeName = a.TypeName,
                           SsalealesVolume = s.SsalealesVolume,
                           CommodityState = s.CommodityState,
                       }).ToList();
            return linq;
        }
        //删除商品
        public int DeleteCommodity(int id)
        {
            var linq = db.Commodities.Find(id);
            if (linq != null)
            {
                linq.ComDelState = 0;
                return db.SaveChanges();
            }
            return -1;
        }
    }
}
